package com.colorlight.cloud.grandshow.entity.enumerate;


import com.colorlight.cloud.all_event.AllProtocolType;
import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.protocol.ProtocolType;
import com.colorlight.cloud.gateway.api.message.constraint.schedule.ScheduleActions;
import com.iot.proto.type.ProtocolSummationType;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import com.colorlight.cloud.grandshow.entity.instructions.*;
import com.colorlight.cloud.grandshow.entity.event.*;
/**
 * @author Demon
 * proto方法类型
 */
@Getter
public enum NameStruct implements ProtocolType {
	/**
	 * 保留消息编号
	 * 加密指令
	 * 解密指令
	 * 定时加密
	 * 获取PC服务器信息
	 */
	RemoveDeviceInstruction(ProtocolSummationType.NameStruct.RemoveDeviceInstruction, RemoveDevice.class),
	EncryptInstruction(ProtocolSummationType.NameStruct.EncryptInstruction, Encrypt.class),
	DecryptInstruction(ProtocolSummationType.NameStruct.DecryptInstruction, Decrypt.class),
	TimingEncryptInstruction(ProtocolSummationType.NameStruct.TimingEncryptInstruction, TimingEncrypt.class),
	GetSenderDeviceInfoInstruction(ProtocolSummationType.NameStruct.GetSenderDeviceInfoInstruction, GetSenderCardsInfo.class),
	/**
	 * 心跳事件
	 * 开机事件
	 * 离线遗嘱
	 * 定时加密执行结果上报
	 */
	
	HBEvent(ProtocolSummationType.NameStruct.HBEvent, HB.class),
	SenderMonitorReportEvent(ProtocolSummationType.NameStruct.SenderMonitorReportEvent, SenderMonitorReport.class),
	OffLineEvent(ProtocolSummationType.NameStruct.OffLineEvent, OffLine.class),
	AuthorizationInfoEvent(ProtocolSummationType.NameStruct.AuthorizationInfoEvent, AuthorizationInfo.class),
	GetMainDeviceInfoEvent(ProtocolSummationType.NameStruct.GetMainDeviceInfoEvent, GetMainDeviceInfo.class),
	;
	
	private final Integer id;
	private final String name;
	private final ProtocolSummationType.NameStruct nameStruct;
	private final Class<? extends ScheduleActions> aClass;
	
	private static final long serialVersionUID = 123456789123456789L;
	
	NameStruct(ProtocolSummationType.NameStruct nameStruct, Class<? extends ScheduleActions> aClass) {
		this.id = nameStruct.getNumber();
		this.name = nameStruct.name();
		this.aClass = aClass;
		this.nameStruct = nameStruct;
	}
	
	
	/**
	 * @param struct 原始类型
	 * @return 对应的名字
	 **/
	
	public static NameStruct getNameStructByProtocolSummationType(ProtocolSummationType.NameStruct struct) {
		Objects.requireNonNull(struct, "struct can not be null.");
		return Arrays.stream(NameStruct.values())
				.filter(nameStruct -> nameStruct.nameStruct.equals(struct))
				.findFirst()
				.orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
	}
	
	
	/**
	 * @param aClass 类型
	 * @return 对应的名字
	 **/
	public static NameStruct getNameStructByClass(Class<? extends ScheduleActions> aClass) {
		Objects.requireNonNull(aClass, "TypeNum can not be null.");
		return Arrays.stream(NameStruct.values())
				.filter(nameStruct -> nameStruct.aClass.equals(aClass))
				.findFirst()
				.orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
	}
	
	/**
	 * @param aClass 类型
	 * @return 对应的名字
	 **/
	public static String getNameByClass(Class<? extends ScheduleActions> aClass) {
		return getNameStructByClass(aClass).getName();
	}
	
	@Override
	public String getInstructTypeName() {
		return AllProtocolType.NAME_STRUCT_GRAND_SHOW.getType();
	}
	
	@Override
	public Class<? extends ScheduleActions> getScheduleClass() {
		return this.aClass;
	}
	
	@Override
	public NameStruct getByProtocolSummationType(ProtocolSummationType.NameStruct nameStruct) {
		return getNameStructByProtocolSummationType(nameStruct);
	}
	@Override
	public String getThisName() {
		return nameStruct.name();
	}
}
